import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Connection, connect } from 'amqplib'
@Injectable()
export class RabbitmqService {
    private connection:Connection;
    constructor(
        private configService:ConfigService,
    ) {
        this.init()
    }

    async init() {
        const options = this.configService.get('rabbitmq.options')
        this.connection = await connect(options)
        console.log('rabbitmq 成功')
    }

    publish(exchange:string, routingKey:string, msg:object, options?:{expiration?:number, timestamp?:number}) {
        this.connection.createChannel().then(channel => {
            channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(msg)), options)
            channel.close()
        })
    }
}
